﻿using System;
using BingoCommonHelper.ConsoleExt;
using BingoGameServerConsole.Common;
using BingoServerCommon.Common;
using HPSocketCS;

namespace BingoGameServerConsole
{
	public partial class BingoTcpPackServer
	{
		/// <summary>
		/// 数据到达事件(指针数据)
		/// </summary>
		/// <param name="connId"></param>
		/// <param name="pData"></param>
		/// <param name="length"></param>
		/// <returns></returns>
		HandleResult OnPointerDataReceive(IntPtr connId, IntPtr pData, int length)
		{
			ConsoleExt.ConsoleWlMethod(System.Reflection.MethodBase.GetCurrentMethod());

			return HandleResult.Ok;
		}


		/// <summary>
		/// 握手成功事件
		/// </summary>
		/// <param name="connId"></param>
		/// <returns></returns>
		HandleResult OnHandShake(IntPtr connId)
		{
			ConsoleExt.ConsoleWlMethod(System.Reflection.MethodBase.GetCurrentMethod());

			return HandleResult.Ok;
		}

		/// <summary>
		/// 准备监听了事件,一般没什么用吧?
		/// </summary>
		/// <param name="soListen"></param>
		/// <returns></returns>
		HandleResult OnPrepareListen(IntPtr soListen)
		{
			Console.BackgroundColor = (ConsoleColor)ConsoleInfoColor.NormalInfoColor;
			Console.WriteLine("服务器准备了监听事件~~~~~~~~~~~");
			Console.ResetColor();
			return HandleResult.Ok;
		}



		/// <summary>
		/// 连接到达事件
		/// </summary>
		/// <param name="connId"></param>
		/// <param name="pClient"></param>
		/// <returns></returns>
		HandleResult OnAccept(IntPtr connId, IntPtr pClient)
		{
			// 客户进入了
			Console.BackgroundColor = (ConsoleColor)ConsoleInfoColor.NormalInfoColor;
			Console.WriteLine("有连接到达~~");
			Console.WriteLine("执行RoomManager里面的room分配和判断 ===>>>> ");
			Console.ResetColor();

			// 获取客户端ip和端口
			string ip = string.Empty;
			ushort port = 0;
			if (Server.GetRemoteAddress(connId, ref ip, ref port))
			{
				Console.WriteLine($" > [{connId},OnAccept] -> PASS({ip}:{port})");
			}
			else
			{
				Console.WriteLine($" > [{connId},OnAccept] -> Server_GetClientAddress() Error");
			}


			// 设置附加数据
			Session session = new Session();
			session.ConnId = connId;
			session.IpAddress = ip;
			session.Port = port;
			session.player = new Player()
			{
				id = 0
			};

			#region 分配房间

			Console.BackgroundColor = (ConsoleColor)ConsoleInfoColor.NormalInfoColor;
			Console.WriteLine("执行RoomManager里面的room分配和判断 ===>>>> ");
			Console.ResetColor();
			//var addResult = RoomManager.AddPlayer(session);
			//if (!addResult)
			//{
			//	Console.BackgroundColor = (ConsoleColor)ConsoleInfoColor.NormalInfoColor;
			//	Console.WriteLine($"RoomManager.AddPlayer出错");
			//	Console.ResetColor();
			//}

			#endregion




			if (Server.SetExtra(connId, session) == false)
			{
				Console.WriteLine($" > [{connId},OnAccept] -> SetConnectionExtra fail");
			}

			return HandleResult.Ok;
		}


		/// <summary>
		/// 数据包发送事件
		/// </summary>
		/// <param name="connId"></param>
		/// <param name="bytes"></param>
		/// <returns></returns>
		HandleResult OnSend(IntPtr connId, byte[] bytes)
		{
			// 服务器发数据了

			//AddMsg(string.Format(" > [{0},OnSend] -> ({1} bytes)", connId, bytes.Length));

			ConsoleExt.ConsoleWlMethod(System.Reflection.MethodBase.GetCurrentMethod());

			return HandleResult.Ok;
		}


		/// <summary>
		/// 数据到达事件
		/// </summary>
		/// <param name="connId"></param>
		/// <param name="bytes"></param>
		/// <returns></returns>
		HandleResult OnReceive(IntPtr connId, byte[] bytes)
		{
			// 数据到达了
			try
			{
				// 获取附加数据
				var session = Server.GetExtra<Session>(connId);
				if (session != null)
				{
					// clientInfo 就是accept里传入的附加数据了
					Console.WriteLine($" > [{session.ConnId},OnReceive] -> {session.IpAddress}:{session.Port} ({bytes.Length} bytes)");
				}
				else
				{
					Console.WriteLine($" > [{connId},OnReceive] -> ({bytes.Length} bytes)");
				}

				int id = NetFunction.ntoh(BitConverter.ToInt32(bytes, 0));
				Package pack = PackageFactory.Instance.NewPackage((short)id, bytes);
				pack.Excute(pack, session);

				return HandleResult.Ok;
			}
			catch (Exception)
			{
				return HandleResult.Error;
			}
		}


		/// <summary>
		/// 连接关闭事件
		/// </summary>
		/// <param name="connId"></param>
		/// <param name="enOperation"></param>
		/// <param name="errorCode"></param>
		/// <returns></returns>
		HandleResult OnClose(IntPtr connId, SocketOperation enOperation, int errorCode)
		{
			if (errorCode == 0)
				Console.WriteLine($" > [{connId},OnClose]");
			else
				Console.WriteLine($" > [{connId},OnError] -> OP:{enOperation},CODE:{errorCode}");
			// return HPSocketSdk.HandleResult.Ok;

			//if (Server.RemoveExtra(connId) == false)
			//{
			//	Console.WriteLine(" > [{0},OnClose] -> SetConnectionExtra({0}, null) fail", connId);
			//}

			//ConsoleExt.ConsoleWlMethod(System.Reflection.MethodBase.GetCurrentMethod());

			var session = Server.GetExtra<Session>(connId);
			//session?.player.Save(Program.ConnectionPool);

			return HandleResult.Ok;
		}


		/// <summary>
		/// 服务器关闭事件
		/// </summary>
		/// <returns></returns>
		HandleResult OnShutdown()
		{
			// 服务关闭了
			ConsoleExt.ConsoleWlMethod(System.Reflection.MethodBase.GetCurrentMethod());
			return HandleResult.Ok;
		}
	}
}
